package chapter2.src.com.kkb.array;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Person {

	private String name;
	private Integer age;

	public Person() {
	}

	public Person(String name, Integer page) {
		this.name = name;
		this.age = page;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}


	@Override
	public boolean equals(Object obj) {

		if ( obj instanceof  Person		){

			String name = ((Person) obj).getName();
			return this.getName().equals(name);
		}else{
			return false;
		}



	}

	@Override
	public String toString() {
		return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
	}

	public static void main(String[] args) {
		List<chapter1.shallowCopy.src.com.kkb.tz.test.Person> personList = new ArrayList<>();
		personList.add(new chapter1.shallowCopy.src.com.kkb.tz.test.Person("李四", 20));
		personList.add(new chapter1.shallowCopy.src.com.kkb.tz.test.Person("张三", 10));
		personList.add(new chapter1.shallowCopy.src.com.kkb.tz.test.Person("王五", 30));
		System.out.println("原始顺序为：" + personList.toString());
		Collections.sort(personList, new Comparator<chapter1.shallowCopy.src.com.kkb.tz.test.Person>() {
			@Override
			public int compare(chapter1.shallowCopy.src.com.kkb.tz.test.Person o1, chapter1.shallowCopy.src.com.kkb.tz.test.Person o2) {
				// 升序
				// return o1.getAge()-o2.getAge();
				// 降序
				return o2.getAge() - o1.getAge();
				// 不变
				// return 0
			}
		});
		System.out.println("排序后顺序为：" + personList.toString());
	}
}
